package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.adapter.AreaAdapter;
import com.bjesc_b2c.app.appraiset.adapter.CityAdapter;
import com.bjesc_b2c.app.appraiset.adapter.ProvinceAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CountryVariousCityBean;

import static com.bjesc_b2c.app.appraiset.utils.TimeListUilts.getCountryCityList;

/**
 * Created by Jason Chen on 2017/2/22.
 * 显示全国城市
 */

public class CountryVariousCityActivity extends BaseActivity {
    @InjectView(R.id.recycler_province)
    RecyclerView provinceRcycler;
    @InjectView(R.id.tv_province)
    TextView provinceTv;
    @InjectView(R.id.recycler_city)
    RecyclerView cityRecycler;
    @InjectView(R.id.tv_city)
    TextView cityTv;
    @InjectView(R.id.recycler_area)
    RecyclerView areaRecycler;
    @InjectView(R.id.tv_county)
    TextView areaTv;

    private ProvinceAdapter provinceAdapter;
    private CityAdapter cityAdapter;
    private AreaAdapter areaAdapter;
    private LinearLayoutManager cityManager;

    private List<CountryVariousCityBean> provinceBeanList;
    private List<CountryVariousCityBean.CityBean> cityBeanList;
    private List<CountryVariousCityBean.CityBean.AreaBean> areaBeanList;
    private ProgressDialog progressDialog;
    private Intent resultIntent;

    private int cityType = 1;   //1 == 全国,  2 == 云南省
    private String provinceId;      //省份ID
    private String provinceName;    //省份名称
    private String cityId;          //城市ID
    private String cityName;        //城市名称
    private String areaId;          //县级ID
    private String areaName;        //县级名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkcar_address);
        cityType = getIntent().getIntExtra("cityType", 1);
        progressDialog = new ProgressDialog(this);
        resultIntent = new Intent();
        getList();
        ButterKnife.inject(this);
    }

    private void getList() {
        progressDialog.show();
        new Thread() {
            @Override
            public void run() {
                if (cityType == 1) {
                    provinceBeanList = getCountryCityList(CountryVariousCityActivity.this, cityType);
                } else {
                    provinceBeanList = getCountryCityList(CountryVariousCityActivity.this, cityType);
                }
                runOnUiThread(initViewRunnable);

            }
        }.start();
    }

    private Runnable initViewRunnable = new TimerTask() {
        @Override
        public void run() {
            progressDialog.dismiss();
            initViews();
        }
    };

    private void initViews() {
        provinceAdapter = new ProvinceAdapter(this, R.layout.layout_item_checkcar_address, provinceBeanList);
        provinceRcycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        provinceRcycler.setAdapter(provinceAdapter);
        provinceAdapter.setOnItemClickListener(provinceListener);
        provinceRcycler.setOnScrollListener(scrollListener);
    }

    /**
     * 推动头部
     */
    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                int firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
                int lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                if (firstVisibleItemPosition == 0 && lastVisibleItemPosition == 0) {
                    return;
                }
                String firstLetter = provinceBeanList.get(firstVisibleItemPosition).getFirstletter();
                int nextPosition = firstVisibleItemPosition + 1;
                if (nextPosition <= provinceBeanList.size()) {
                    String nextLetter = provinceBeanList.get(nextPosition).getFirstletter();
                    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) provinceTv.getLayoutParams();

                    if (!nextLetter.equals(firstLetter)) {
                        View childView = recyclerView.getChildAt(0);
                        if (childView == null) {
                            return;
                        }
                        int offSetY = childView.getBottom() - provinceTv.getHeight();
                        if (offSetY < 0) {
                            layoutParams.topMargin = offSetY;
                            provinceTv.setLayoutParams(layoutParams);
                        } else {
                            layoutParams.topMargin = 0;
                            provinceTv.setLayoutParams(layoutParams);
                        }
                    } else {
                        layoutParams.topMargin = 0;
                        provinceTv.setLayoutParams(layoutParams);
                    }
                    provinceTv.setText(nextLetter);
                }

            }
        }
    };

    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }

    /**
     * 省份Item点击事件
     */
    private MultiItemTypeAdapter.OnItemClickListener provinceListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            cityBeanList = provinceBeanList.get(position).getCity();
            for (int i = 0; i < provinceBeanList.size(); i++) {
                if (i == position) {
                    provinceBeanList.get(i).setCheck(true);
                } else {
                    provinceBeanList.get(i).setCheck(false);
                }
            }
            cityTv.setText(provinceBeanList.get(position).getProvince());
            provinceName = provinceBeanList.get(position).getProvince();
            provinceId = provinceBeanList.get(position).getProvinceid();
            if (cityBeanList == null || cityBeanList.size() == 0) {
                resultIntent.putExtra("provinceId", provinceId);
                resultIntent.putExtra("provinceName", provinceName);
                if(cityType == 1){
                    setResult(RequestAndResultCode.CountryVariousCityActivity_ResultCode_National, resultIntent);
                }else{
                    setResult(RequestAndResultCode.CountryVariousCityActivity_ResultCode_Area, resultIntent);
                }
                finish();
            }
            if (cityAdapter == null) {
                cityAdapter = new CityAdapter(CountryVariousCityActivity.this, R.layout.layout_item_checkcar_address, cityBeanList);
                cityManager = new LinearLayoutManager(CountryVariousCityActivity.this, LinearLayoutManager.VERTICAL, false);
                cityRecycler.setLayoutManager(cityManager);
                cityRecycler.setAdapter(cityAdapter);
                cityAdapter.setOnItemClickListener(cityListener);
            } else {
                cityAdapter.refresh(cityBeanList);
                if (areaAdapter != null && areaBeanList != null) {
                    areaAdapter.refresh(new ArrayList<CountryVariousCityBean.CityBean.AreaBean>());
                }
            }
            provinceAdapter.notifyDataSetChanged();
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };

    /**
     * 城市Item点击事件
     */
    private MultiItemTypeAdapter.OnItemClickListener cityListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            areaBeanList = cityBeanList.get(position).getArea();
            for (int i = 0; i < cityBeanList.size(); i++) {
                if (i == position) {
                    cityBeanList.get(i).setCheck(true);
                } else {
                    cityBeanList.get(i).setCheck(false);
                }
            }
            areaTv.setText(cityBeanList.get(position).getCity());
            cityId = cityBeanList.get(position).getCityid();
            cityName = cityBeanList.get(position).getCity();
//            if (areaBeanList == null || areaBeanList.size() == 0) {
                resultIntent.putExtra("provinceId", provinceId);
                resultIntent.putExtra("provinceName", provinceName);
                resultIntent.putExtra("cityId", cityId);
                resultIntent.putExtra("cityName", cityName);
                if(cityType == 1){
                    setResult(RequestAndResultCode.CountryVariousCityActivity_ResultCode_National, resultIntent);
                }else{
                    setResult(RequestAndResultCode.CountryVariousCityActivity_ResultCode_Area, resultIntent);
                }
                finish();
//            }

            if (areaAdapter == null) {
                areaAdapter = new AreaAdapter(CountryVariousCityActivity.this, R.layout.layout_item_checkcar_address, areaBeanList);
                areaRecycler.setLayoutManager(new LinearLayoutManager(CountryVariousCityActivity.this, LinearLayoutManager.VERTICAL, false));
                areaRecycler.setAdapter(areaAdapter);
                areaAdapter.setOnItemClickListener(countyListener);
            } else {

                areaAdapter.refresh(areaBeanList);
            }
            cityAdapter.notifyDataSetChanged();
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };

    private MultiItemTypeAdapter.OnItemClickListener countyListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            areaId = areaBeanList.get(position).getAreaid();
            areaName = areaBeanList.get(position).getArea();
            resultIntent.putExtra("provinceId", provinceId);
            resultIntent.putExtra("provinceName", provinceName);
            resultIntent.putExtra("cityId", cityId);
            resultIntent.putExtra("cityName", cityName);
            resultIntent.putExtra("areaId", areaId);
            resultIntent.putExtra("areaName", areaName);
            if(cityType == 1){
                setResult(RequestAndResultCode.CountryVariousCityActivity_ResultCode_National, resultIntent);
            }else{
                setResult(RequestAndResultCode.CountryVariousCityActivity_ResultCode_Area, resultIntent);
            }
            finish();
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };
}
